package com.the4man.framework.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by lenovo on 2017/12/12.
 */
public class DateUtil {
    private static final Logger logger = LoggerFactory.getLogger(DateUtil.class);
    private static DateUtil ldu_ = null;

    public static DateUtil getInstance() {
        if(ldu_ == null) {
            Class var0 = DateUtil.class;
            synchronized(DateUtil.class) {
                if(ldu_ == null) {
                    ldu_ = new DateUtil();
                }
            }
        }

        return ldu_;
    }

    private DateUtil() {
    }

    public String getThisMonthStartDay(String dateStr) {
        return dateStr.substring(0, dateStr.length() - 2) + "01";
    }

    public String getLastWeekDate(String dateStr, String theFormat) {
        return this.convertDate(dateStr, theFormat, 6, -7);
    }

    public String getLastDate(String dateStr, String theFormat) {
        return this.convertDate(this.getThisMonthStartDay(dateStr), theFormat, 5, -1);
    }

    public String getLastMonth(String acycID, String theFormat) {
        return this.convertDate(acycID.substring(0, 4) + acycID.substring(4, 6), theFormat, 2, -1).substring(0, 6);
    }

    public String getLastHour(String acycID, String theFormat) {
        Calendar c = Calendar.getInstance();
        Date date = null;

        try {
            date = (new SimpleDateFormat(theFormat)).parse(acycID);
        } catch (ParseException var7) {
            var7.printStackTrace();
        }

        c.setTime(date);
        int hour = c.get(10);
        c.set(10, hour - 1);
        String hourBefore = (new SimpleDateFormat(theFormat)).format(c.getTime());
        return hourBefore;
    }

    public String getLastYear(String acycID, String theFormat) {
        Calendar c = Calendar.getInstance();
        Date date = null;

        try {
            date = (new SimpleDateFormat(theFormat)).parse(acycID);
        } catch (ParseException var7) {
            var7.printStackTrace();
        }

        c.setTime(date);
        int year = c.get(1);
        c.set(1, year - 1);
        String yearBefore = (new SimpleDateFormat(theFormat)).format(c.getTime());
        return yearBefore;
    }

    public String getLastMonthDate(String acycID, String theFormat) {
        return this.convertDate(acycID, theFormat, 2, -1).substring(0, 8);
    }

    public String getLastYearDate(String acycID, String theFormat) {
        return this.convertDate(acycID, theFormat, 1, -1).substring(0, 8);
    }

    public String getLastMonthStartDay(String dateStr, String theFormat) {
        return this.convertDate(this.getThisMonthStartDay(dateStr), theFormat, 2, -1);
    }

    public String getLastMonthEndDay(String dateStr, String theFormat) {
        return this.convertDate(this.getThisMonthStartDay(dateStr), theFormat, 6, -1);
    }

    public String getToday(String theFormat) {
        SimpleDateFormat formatter = new SimpleDateFormat(theFormat, Locale.CHINESE);
        Date thedate = new Date();
        return formatter.format(thedate);
    }

    public String getYesterday(String todaystr, String theFormat) {
        return this.convertDate(todaystr, theFormat, 6, -1);
    }

    public String convertDate(String dateStr, String theFormat, int feildnum, int thenum) {
        SimpleDateFormat formatter = null;
        Calendar cldr = null;
        Date date_pre = null;
        Date date1 = null;

        try {
            formatter = new SimpleDateFormat(theFormat, Locale.CHINESE);
            cldr = new GregorianCalendar();
            date1 = formatter.parse(dateStr);
            cldr.setTime(date1);
            cldr.add(feildnum, thenum);
            date_pre = cldr.getTime();
        } catch (Exception var10) {
            ;
        }

        return date_pre == null?formatter.format(date1):formatter.format(date_pre);
    }

    public int calDate(String dateStr1, String datestr2, String theFormat) {
        SimpleDateFormat formatter = null;
        Date date1 = null;
        Date date2 = null;
        int num = 0;

        try {
            formatter = new SimpleDateFormat(theFormat, Locale.CHINESE);
            date1 = formatter.parse(dateStr1);
            date2 = formatter.parse(datestr2);
            num = Integer.parseInt(String.valueOf((date1.getTime() - date2.getTime()) / 1000L / 60L / 60L / 24L));
        } catch (Exception var9) {
            ;
        }

        return num;
    }

    public Vector betweenDates(String dateStr1, String datestr2, String theFormat) {
        Vector v1 = new Vector();
        int checknum = this.calDate(datestr2, dateStr1, theFormat);
        if(checknum <= 0) {
            if(checknum == 0) {
                v1.add(dateStr1);
                return v1;
            } else {
                return null;
            }
        } else {
            for(int i = 0; i <= checknum; ++i) {
                String datestr = this.convertDate(dateStr1, "yyyyMMdd", 6, i);
                v1.add(datestr);
            }

            return v1;
        }
    }

    public Vector betweenMons(String fromDate, String toDate) {
        try {
            if(Double.parseDouble(fromDate) > Double.parseDouble(toDate)) {
                return null;
            } else {
                Vector v1 = new Vector();
                String currMonth = fromDate;
                v1.add(fromDate);

                while(!currMonth.equals(toDate)) {
                    currMonth = this.convertDate(currMonth, "yyyyMM", 2, 1);
                    v1.add(currMonth);
                }

                return v1;
            }
        } catch (Exception var5) {
            logger.error(var5.toString());
            return null;
        }
    }

    public Vector<String> betweenHours(String formHour, String toHour) {
        try {
            if(Double.parseDouble(formHour) > Double.parseDouble(toHour)) {
                return null;
            } else {
                Vector<String> v1 = new Vector();
                String currHour = formHour;
                v1.add(formHour);

                while(!currHour.equals(toHour)) {
                    currHour = this.convertDate(currHour, "yyyyMMddHH", 10, 1);
                    v1.add(currHour);
                }

                return v1;
            }
        } catch (Exception var5) {
            logger.error("获取时间间隔之间的小时异常", var5);
            return null;
        }
    }

    public List<String> getBeforeSevenHour(int currentHour, String theFormat) {
        List<String> finalList = new ArrayList();
//        int beforeT = false;
        int beforeT;
        if(currentHour < 7) {
            beforeT = 18 + currentHour;
        } else {
            beforeT = currentHour - 6;
        }

        for(int idx = 0; idx < 7; ++idx) {
            String finalStr = "";
            String temp = "";
            if(beforeT + 1 > 25) {
                beforeT = 1;
                temp = "0" + beforeT;
                finalStr = theFormat + temp;
            } else if(beforeT < 10) {
                temp = "0" + beforeT;
                finalStr = theFormat + temp;
            } else {
                finalStr = theFormat + beforeT;
            }

            finalList.add(finalStr);
            ++beforeT;
        }

        return finalList;
    }

    public String formatDate(Date date) {
        return this.formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    public String formatDate(Date date, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        String dateStr = df.format(date);
        return dateStr;
    }

    public static void main(String[] args) {
        Vector v = getInstance().betweenHours("2013071523", "2013071802");
    }

    public String convertDay_Type(String str_d, String Format_Old, String Format_New) {
        SimpleDateFormat sdf = new SimpleDateFormat(Format_Old, Locale.CHINESE);
        SimpleDateFormat sdf2 = new SimpleDateFormat(Format_New, Locale.CHINESE);
        String Str_date = "";

        try {
            Date d1 = sdf.parse(str_d);
            Str_date = sdf2.format(d1);
        } catch (Exception var8) {
            ;
        }

        return Str_date;
    }

    public String convertDay_Week(String dateStr, int thenum) {
        SimpleDateFormat formatter = null;
        Calendar cldr = null;
        Date date_pre = null;
        Date date1 = null;

        try {
            formatter = new SimpleDateFormat("yyyyMMdd", Locale.CHINESE);
            cldr = new GregorianCalendar();
            date1 = formatter.parse(dateStr);
            cldr.setTime(date1);
            int iWeek = cldr.get(7);
//            int i = false;
            int i;
            if(iWeek == 1) {
                i = thenum - iWeek - 6;
            } else {
                i = thenum - iWeek + 1;
            }

            cldr.add(6, i);
            date_pre = cldr.getTime();
        } catch (Exception var9) {
            ;
        }

        return date_pre == null?formatter.format(date1):formatter.format(date_pre);
    }

    public int getDayOfWeek(String dateStr, String theFormat) {
        SimpleDateFormat formatter = new SimpleDateFormat(theFormat, Locale.CHINESE);
        Calendar cldr = null;
        Date date1 = null;
        int iWeek = 0;

        try {
            cldr = new GregorianCalendar();
            date1 = formatter.parse(dateStr);
            cldr.setTime(date1);
            iWeek = cldr.get(7);
        } catch (Exception var8) {
            ;
        }

        return iWeek;
    }

    public String getQuarterName(String sDate) {
        String sQuarter = "";
        if(sDate.length() < 6) {
            return "当前日期有误：" + sDate;
        } else {
            String sYear = sDate.substring(0, 4) + "年";
            int iMM = Integer.parseInt(sDate.substring(4, 6));
            switch(iMM) {
                case 1:
                    sQuarter = sYear + "第一季度";
                    break;
                case 2:
                    sQuarter = sYear + "第一季度";
                    break;
                case 3:
                    sQuarter = sYear + "第一季度";
                    break;
                case 4:
                    sQuarter = sYear + "第二季度";
                    break;
                case 5:
                    sQuarter = sYear + "第二季度";
                    break;
                case 6:
                    sQuarter = sYear + "第二季度";
                    break;
                case 7:
                    sQuarter = sYear + "第三季度";
                    break;
                case 8:
                    sQuarter = sYear + "第三季度";
                    break;
                case 9:
                    sQuarter = sYear + "第三季度";
                    break;
                case 10:
                    sQuarter = sYear + "第四季度";
                    break;
                case 11:
                    sQuarter = sYear + "第四季度";
                    break;
                case 12:
                    sQuarter = sYear + "第四季度";
            }

            return sQuarter;
        }
    }

    public String getQuarterCode(String sDate) {
        String sQuarter = "";
        if(sDate.length() < 6) {
            return "当前日期有误：" + sDate;
        } else {
            String sYear = sDate.substring(0, 4);
            int iMM = Integer.parseInt(sDate.substring(4, 6));
            switch(iMM) {
                case 1:
                    sQuarter = sYear + "1";
                    break;
                case 2:
                    sQuarter = sYear + "1";
                    break;
                case 3:
                    sQuarter = sYear + "1";
                    break;
                case 4:
                    sQuarter = sYear + "2";
                    break;
                case 5:
                    sQuarter = sYear + "2";
                    break;
                case 6:
                    sQuarter = sYear + "2";
                    break;
                case 7:
                    sQuarter = sYear + "3";
                    break;
                case 8:
                    sQuarter = sYear + "3";
                    break;
                case 9:
                    sQuarter = sYear + "3";
                    break;
                case 10:
                    sQuarter = sYear + "4";
                    break;
                case 11:
                    sQuarter = sYear + "4";
                    break;
                case 12:
                    sQuarter = sYear + "4";
            }

            return sQuarter;
        }
    }

    public String getLastQuaMonth(String sDate) {
        String sQuarter = "";
        if(sDate.length() < 6) {
            return "当前日期有误：" + sDate;
        } else {
            if(sDate.length() > 6) {
                sDate = sDate.substring(0, 6);
            }

            int iMM = Integer.parseInt(sDate.substring(4, 6));
            switch(iMM) {
                case 1:
                    sQuarter = this.convertDate(sDate, "yyyyMM", 2, -1);
                    break;
                case 2:
                    sQuarter = this.convertDate(sDate, "yyyyMM", 2, -2);
                    break;
                case 3:
                    sQuarter = sDate;
                    break;
                case 4:
                    sQuarter = this.convertDate(sDate, "yyyyMM", 2, -1);
                    break;
                case 5:
                    sQuarter = this.convertDate(sDate, "yyyyMM", 2, -2);
                    break;
                case 6:
                    sQuarter = this.convertDate(sDate, "yyyyMM", 2, -3);
                    break;
                case 7:
                    sQuarter = this.convertDate(sDate, "yyyyMM", 2, -1);
                    break;
                case 8:
                    sQuarter = this.convertDate(sDate, "yyyyMM", 2, -2);
                    break;
                case 9:
                    sQuarter = sDate;
                    break;
                case 10:
                    sQuarter = this.convertDate(sDate, "yyyyMM", 2, -1);
                    break;
                case 11:
                    sQuarter = this.convertDate(sDate, "yyyyMM", 2, -2);
                    break;
                case 12:
                    sQuarter = sDate;
            }

            return sQuarter;
        }
    }

    public String getQuarMonth(String sQuarter, String sFlag) {
        String sDate = "-1";
        if(sQuarter.length() != 5) {
            logger.error("传入的季度值有误：" + sQuarter);
            return "-1";
        } else {
            String sYear = sQuarter.substring(0, 4);
            int iNum = Integer.parseInt(sQuarter.substring(4, 5));
            switch(iNum) {
                case 1:
                    if("begin".equals(sFlag)) {
                        sDate = sYear + "01";
                    } else if("end".equals(sFlag)) {
                        sDate = sYear + "03";
                    }
                    break;
                case 2:
                    if("begin".equals(sFlag)) {
                        sDate = sYear + "04";
                    } else if("end".equals(sFlag)) {
                        sDate = sYear + "06";
                    }
                    break;
                case 3:
                    if("begin".equals(sFlag)) {
                        sDate = sYear + "07";
                    } else if("end".equals(sFlag)) {
                        sDate = sYear + "09";
                    }
                    break;
                case 4:
                    if("begin".equals(sFlag)) {
                        sDate = sYear + "10";
                    } else if("end".equals(sFlag)) {
                        sDate = sYear + "12";
                    }
            }

            return sDate;
        }
    }

    public String getDefaultDate(String sTime, String dateType) {
        String tmpDate = "";

        try {
            if("month".equals(dateType)) {
                if(Integer.parseInt(this.getToday("ddHHmm")) >= Integer.parseInt(sTime)) {
                    tmpDate = this.convertDate(this.getToday("yyyyMM"), "yyyyMM", 2, -1);
                } else {
                    tmpDate = this.convertDate(this.getToday("yyyyMM"), "yyyyMM", 2, -2);
                }
            } else if(Integer.parseInt(this.getToday("HHmm")) >= Integer.parseInt(sTime)) {
                tmpDate = this.getYesterday(this.getToday("yyyyMMdd"), "yyyyMMdd");
            } else {
                tmpDate = this.convertDate(this.getToday("yyyyMMdd"), "yyyyMMdd", 6, -2);
            }
        } catch (Exception var5) {
            logger.error(var5.toString());
            if("month".equals(dateType)) {
                if(Integer.parseInt(this.getToday("ddHHmm")) >= '월') {
                    tmpDate = this.convertDate(this.getToday("yyyyMM"), "yyyyMM", 2, -1);
                } else {
                    tmpDate = this.convertDate(this.getToday("yyyyMM"), "yyyyMM", 2, -2);
                }
            } else if(Integer.parseInt(this.getToday("HHmm")) >= 900) {
                tmpDate = this.getYesterday(this.getToday("yyyyMMdd"), "yyyyMMdd");
            } else {
                tmpDate = this.convertDate(this.getToday("yyyyMMdd"), "yyyyMMdd", 6, -2);
            }
        }

        return tmpDate;
    }

    public String getXDayBeforeToday(String todaystr, String theFormat, int num) {
        return this.convertDate(todaystr, theFormat, 6, num);
    }

    public String getTimeStamp() {
        synchronized(this) {
            Date nowTime = new Date();
            SimpleDateFormat timeStamp = new SimpleDateFormat("yyyyMMddHHmmss");
            String nowTimeStamp = timeStamp.format(nowTime);
            String haomiao = String.valueOf(System.nanoTime());
            nowTimeStamp = nowTimeStamp + haomiao.substring(haomiao.length() - 6, haomiao.length());
            return nowTimeStamp;
        }
    }

    public String getTime() {
        synchronized(this) {
            String nowTimeStamp = String.valueOf(System.nanoTime());
            nowTimeStamp = nowTimeStamp.substring(nowTimeStamp.length() - 6, nowTimeStamp.length());
            return nowTimeStamp;
        }
    }

    public Timestamp getSqlTimeStamp() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        Timestamp ts = Timestamp.valueOf(time);
        return ts;
    }

    public String getSqlTimeString() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        return time;
    }

    public String getYMDTimeStamp() {
        Date nowTime = new Date();
        SimpleDateFormat timeStamp = new SimpleDateFormat("yyyyMMdd");
        String nowTimeStamp = timeStamp.format(nowTime);
        return nowTimeStamp;
    }
}
